package io.geekidea.springbootplus.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.geekidea.springbootplus.framework.common.service.impl.BaseServiceImpl;

import io.geekidea.springbootplus.framework.core.pagination.PageInfo;
import io.geekidea.springbootplus.framework.core.pagination.Paging;
import io.geekidea.springbootplus.framework.util.PhoneUtil;
import io.geekidea.springbootplus.system.entity.SysAddress;
import io.geekidea.springbootplus.system.mapper.SysAddressMapper;
import io.geekidea.springbootplus.system.param.SysAddressPageParam;
import io.geekidea.springbootplus.system.service.SysAddressService;
import io.geekidea.springbootplus.system.vo.SysAddressQueryVo;
import io.geekidea.springbootplus.system.vo.SysMemberQueryVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class SysAddressServiceImpl extends BaseServiceImpl<SysAddressMapper, SysAddress> implements SysAddressService {
    @Resource
    private SysAddressMapper sysAddressMapper;

    @Override
    public Paging<SysAddressQueryVo> getSysAddressPageList(SysAddressPageParam sysAddressPageParam) throws Exception {
        Page<SysAddressQueryVo> page = new PageInfo<>(sysAddressPageParam);
        IPage<SysAddressQueryVo> iPage = sysAddressMapper.getSysAddressPageList(page, sysAddressPageParam);

        // 手机号码脱敏处理
        if (iPage != null && CollectionUtils.isNotEmpty(iPage.getRecords())) {
            iPage.getRecords().forEach(vo -> {
                vo.setPhone(PhoneUtil.desensitize(vo.getPhone()));
            });
        }
        return new Paging(iPage);
    }

    @Override
    public SysAddressQueryVo selectAddressById(Integer id) throws Exception {
        SysAddressQueryVo sysAddress = sysAddressMapper.selectAddressById(id);
        return sysAddress;
    }

    @Override
    public List<SysAddressQueryVo> getAddressListByMemberId(Integer memberId) throws Exception {
        List<SysAddressQueryVo> sysAddressList = sysAddressMapper.getSysAddressByMemberId(memberId);
        return sysAddressList;
    }

    @Override
    public boolean updateDefaultAddress(Integer memberId, Integer id) throws Exception {
        SysAddress defaultAddress = sysAddressMapper.getDefaultAddress(memberId);
        if (defaultAddress.getId() != id ){
            defaultAddress.setIsDefault(0);
            System.out.println(defaultAddress);
            sysAddressMapper.updateById(defaultAddress);
            Integer result = sysAddressMapper.updateDefaultAddress(id);
            return result>0;
        }else {
            return true;
        }
    }

    @Override
    public boolean deleteAddressBatchIds(List<Integer> idList) {
        int result = sysAddressMapper.deleteAddressByBatch(idList);
        return result>0;
    }

    @Override
    public boolean deleteAddress(Integer id) {
        int result = sysAddressMapper.deleteAddress(id);
        return result >0;
    }

    @Override
    public boolean addAddress(SysAddress sysAddress) {
        Integer count = sysAddressMapper.getAddressCount(sysAddress.getMemberId());
        System.out.println(count);
        System.out.println(sysAddress);
        /*设置第一个地址为默认地址*/
        if (count == 0){
            sysAddress.setIsDefault(1);
            Integer result = sysAddressMapper.addAddress(sysAddress);
            System.out.println("1"+sysAddress);
            return result >0;
        }else {
            Integer result = sysAddressMapper.addAddress(sysAddress);
            System.out.println("2"+sysAddress);
            return result >0;
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateAddress(SysAddress sysAddress)  {
        return super.updateById(sysAddress);
    }
}
